//
// Created by liushuai on 18-9-25.
//

#include "List.h"
#include "Node.h"
List::List() {
    head = new Node;
    head->data = 0;
    head->next = nullptr;
    mLength = 0;
}
bool List::ListEmpty() {
    if(mLength == 0)
        return true;
    else
        return false;
}
int List::ListLength() {
    return mLength;
}
void List::ClearList() {
    Node* CurrentNode = head->next;
    while(CurrentNode != nullptr){
        Node *temp = CurrentNode->next;
        delete temp;
        CurrentNode = temp;
    }
    head->next = nullptr;
}
List::~List() {
    ClearList();
    delete head;
    head->next = nullptr;
}
bool List::ListInsertHead(Node *pNode) {
    Node* temp = head->next;
    Node* newNode = new Node;
    if(newNode == nullptr)
        return false;
    newNode->data = head->data;
    head->next = newNode;
    newNode->next = temp;
    return true;
}
bool List::ListInsertTail(Node *pNode) {
    Node* newNode = new Node;
    if(newNode == nullptr)
        return false;
    tail->next = newNode;
    newNode->data = pNode->data;
    newNode->next = nullptr;
    return true;
}
bool List::ListInsert(unsigned int i, Node *pNode) {
    if(i>mLength)
        return false;
    Node* CurrentNode = head;
    for(int j=0;j<i;j++)
    {
        CurrentNode = head->next;
    }
    Node* temp = new Node;
    if(temp == nullptr)
        return false;
    temp->data = pNode->data;
    temp->next = CurrentNode->next;
    CurrentNode->next = temp;
    mLength++;
}
bool List::ListDelete(unsigned int i, Node *pNode) {
    if(i>mLength)
        return false;
    Node* Current_Node = head;
    Node* Current_pre = nullptr;
    for(int k = 0;k<=i;k++)
    {
        Current_pre = Current_Node;
        Current_Node = Current_Node->next;
    }
    Current_pre->next = Current_Node->next;
    pNode->data = Current_Node->data;
    delete  Current_Node;
    Current_Node = nullptr;
    mLength--;
    return true;
}
bool List::GetElem(int i, Node *pNode) {
    if(i>mLength)
        return false;
    Node* CurrentNode = head;
    Node* CurrentNode_pre = nullptr;
    for(int k=0;k<i;k++)
    {
        CurrentNode_pre = CurrentNode;
        CurrentNode = CurrentNode_pre->next;
    }
    pNode->data = CurrentNode->data;
    return true;

}
int List::LocateEmen(Node *pNode) {
    Node* currentNode = head;
    int count = 0;
    while(currentNode->next!= nullptr)
    {
        currentNode = currentNode->next;
        if(currentNode->data == pNode->data)
            return count;
        count++;
    }
    return -1;
}
bool List::PriorElem(Node *pCurrentNode, Node *pPreNode) {
    Node* CurrentNode = head;
    Node* tempNode = nullptr;
    while(CurrentNode->next!=nullptr)
    {
        tempNode = CurrentNode;
        CurrentNode = tempNode->next;
        if(CurrentNode == pCurrentNode->next)
        {
            if(tempNode == head)
                return false;
            pPreNode->data = CurrentNode->data;
            return true;
        }
    }
    return false;
}
bool List::NextElem(Node *pCurrentNode, Node *pNextNode) {
    Node* CurrentNode = head;
    while(CurrentNode->next != nullptr)
    {
        CurrentNode = CurrentNode->next;
        if(CurrentNode->data == pCurrentNode->data)
        {
            if(CurrentNode->next == nullptr)
                return false;
            pNextNode->data = CurrentNode->next->data;
            return true;
        }
    }
    return false;
}
void List::ListTraverse() {
    Node* Current_Node = head;
    while(Current_Node->next!=nullptr)
    {
        Current_Node = Current_Node->next;
        Current_Node->Print_Node();
    }
}